<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="oracle.jrockit.jfr.openmbean.ProducerDescriptorType" %>
<%@ page import="com.entity.Product" %>
<%@ page import="java.util.Set" %><%--
  Created by IntelliJ IDEA.
  User: webrx
  Date: 2017/6/18
  Time: 16:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>购物车</title>
</head>
<body>
<%
    //更新购物车商品数量或信息
    if(request.getParameter("update")!=null){
        HashMap hm = (HashMap) session.getAttribute("shop");
        Iterator itm =hm.values().iterator();
        while(itm.hasNext()){
            Product pp = (Product) itm.next();
            int pnum=Integer.parseInt(request.getParameter(pp.getPno()));
            pp.setCount(pnum);
        }
    }
    //从购物车删除商品
    if(request.getParameter("delpno")!=null){
        HashMap temp =(HashMap)session.getAttribute("shop");
        temp.remove(request.getParameter("delpno"));
    }

    //向购物车中添加新商品
    if(request.getParameter("id")!=null){
        String pno =request.getParameter("id");
        String pname=request.getParameter("pname");
        double p =Double.parseDouble(request.getParameter("price"));

        Product myp = new Product();
        myp.setPno(pno);
        myp.setCount(1);
        myp.setP(p);
        myp.setPname(pname);

        if(session.getAttribute("shop")!=null){
           HashMap temp = (HashMap) session.getAttribute("shop");
           if(temp.get(pno)!=null){
               int t =((Product)temp.get(pno)).getCount();
              ((Product) temp.get(pno)).setCount(t+1);
           }else{
              temp.put(pno,myp);
           }

        }else {
            HashMap<String,Product> shopbag = new HashMap<String, Product>();
            shopbag.put(pno,myp);
            session.setAttribute("shop",shopbag);
        }

    }
    HashMap hm = (HashMap) session.getAttribute("shop");
   /* out.print(hm.size());*/
   /* Set<String> key= hm.keySet();
    Iterator<String> it = key.iterator();
    while(it.hasNext()){

       Object ss = it.next();
      Product ppp = (Product) hm.get(ss);
        System.out.println(ppp.getP());
    }*/
    Iterator itm = hm.values().iterator();
%>
<form action="bay_over.jsp" name="form1" method="post">
    <%
        double d=0;
        while(itm.hasNext()){
            Product pp = (Product) itm.next();
            d+=pp.getP()*pp.getCount();

    %>
    <div>
        编号：<%=pp.getPno()%>&nbsp;
        名称：<%=pp.getPname()%>&nbsp;
        数量：<input type="text" name="<%=pp.getPno()%>" value="<%=pp.getCount()%>" >件&nbsp;
        单价：<%=pp.getP()%>元&nbsp;
        汇总：<%=pp.getP()*pp.getCount()%> 元
        <a href="bay_shoping.jsp?delpno=<%=pp.getPno()%>">删除</a><br><br>
    </div>
    <%
        }
    %>
    <a href="bay_over.jsp">去收银台</a><br><br>
</form>
<hr>
共计金额：<%=d%> 元
<hr>
<a href="bay_shoping.jsp">查看购物车</a><br><br>
<a href="bay_clear.jsp">清空购物车</a><br><br>

<a href="../index01.jsp">购买商品</a><br><br>
<a href="#" onclick="document.form1.action='bay_shoping.jsp?update=yes';document.form1.submit()">更新购物车</a>


</body>
</html>
